package cn.cosmosx.base.util;

import org.apache.commons.lang3.StringUtils;

import java.security.SecureRandom;
import java.util.Collection;
import java.util.Map;

/**
 * 工具类
 */
public class ObjectsUtil {
    final static SecureRandom RANDOM = new SecureRandom();

    /**
     * 判断输入参数不为空
     *
     * @param value - 输入参数
     * @return - notNull/true;null/false
     */
    public static boolean isNotNull(Object value) {
        return !isNull(value);
    }

    /**
     * 判断输入参数不为空
     *
     * @param value - 输入参数
     * @return - notNull/false;null/true
     */
    public static boolean isNull(Object value) {
        if (value == null) {
            return true;
        }
        if (value instanceof String) {
            return StringUtils.isBlank((String) value);
        } else if (value instanceof Map) {
            return ((Map<?, ?>) value).isEmpty();
        } else if (value instanceof Collection) {
            return ((Collection<?>) value).isEmpty();
        } else if (value instanceof Object[]) {
            return ((Object[]) value).length == 0;
        }
        return false;
    }

    /**
     * 数字随机数
     *
     * @param length 随机数长度
     * @return
     */
    public static String randomNumber(int length) {
        StringBuilder retVal = new StringBuilder();
        for (int i = 0; i < length; i++) {
            retVal.append(RANDOM.nextInt(10));
        }
        return retVal.toString();
    }

    /**
     * 字母随机数
     *
     * @param length 随机数长度
     * @return
     */
    public static String randomLetter(int length) {
        char[] letters = "ABCDEFGHIJKLMNOPKRSTUVWXYZabcdefghijklmnopkrstuvwxyz".toCharArray();
        StringBuilder retVal = new StringBuilder();
        for (int i = 0; i < length; i++) {
            retVal.append(letters[RANDOM.nextInt(letters.length)]);
        }
        return retVal.toString();
    }

    /**
     * 数字字母混合随机数
     *
     * @param length 随机数长度
     * @return
     */
    public static String randomMix(int length) {
        char[] numberMixLetters = "0123456789ABCDEFGHIJKLMNOPKRSTUVWXYZabcdefghijklmnopkrstuvwxyz".toCharArray();
        StringBuilder retVal = new StringBuilder();
        for (int i = 0; i < length; i++) {
            retVal.append(numberMixLetters[RANDOM.nextInt(numberMixLetters.length)]);
        }
        return retVal.toString();
    }

}
